import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class IconGenerator {
    public static void generateIcon(String backgroundPath, String logoPath, String brandName, String slogan, String contact, String outputPath) throws IOException {
        BufferedImage background = ImageIO.read(new File(backgroundPath));
        BufferedImage logo = ImageIO.read(new File(logoPath));

        int width = background.getWidth();
        int height = background.getHeight();

        BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = output.createGraphics();

        // 开启抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        // 绘制背景
        g.drawImage(background, 0, 0, width, height, null);

        int contentHeight = (int) (height * 0.6); // 仅占背景的 60%
        int startY = (height - contentHeight) / 2;
        int padding = contentHeight / 50; // 适当的间距

        // 计算 logo 位置和大小
        int logoMaxWidth = width / 3;
        int logoMaxHeight = contentHeight / 6;
        int logoWidth = Math.min(logoMaxWidth, logo.getWidth());
        int logoHeight = Math.min(logoMaxHeight, logo.getHeight());
        int logoX = (width - logoWidth) / 2;
        int logoY = startY + padding;

        // 绘制 logo
        g.drawImage(logo, logoX, logoY, logoWidth, logoHeight, null);

        // 计算文字区域大小
        int textAreaHeight = contentHeight - logoHeight - 3 * padding;
        int brandNameHeight = textAreaHeight / 2;
        int sloganHeight = textAreaHeight / 4;
        int contactHeight = textAreaHeight / 4;

        // 绘制品牌名称（竖排）
        Font brandFont = new Font("SansSerif", Font.BOLD, brandNameHeight / brandName.length());
        g.setFont(brandFont);
        g.setColor(Color.BLACK);
        drawVerticalText(g, brandName, width / 6, logoY + logoHeight + padding, brandNameHeight);

        // 绘制宣传标语
        Font sloganFont = new Font("SansSerif", Font.PLAIN, sloganHeight / 2);
        g.setFont(sloganFont);
        drawCenteredText(g, slogan, width / 2, logoY + logoHeight + padding + brandNameHeight + padding);

        // 绘制联系方式
        Font contactFont = new Font("SansSerif", Font.PLAIN, contactHeight / 2);
        g.setFont(contactFont);
        drawCenteredText(g, contact, width / 2, startY + contentHeight - padding - contactHeight);

        g.dispose();
        ImageIO.write(output, "png", new File(outputPath));
    }

    private static void drawCenteredText(Graphics2D g, String text, int x, int y) {
        FontMetrics fm = g.getFontMetrics();
        Rectangle2D rect = fm.getStringBounds(text, g);
        int textX = x - (int) rect.getWidth() / 2;
        int textY = y + fm.getAscent() / 2;
        g.drawString(text, textX, textY);
    }

    private static void drawVerticalText(Graphics2D g, String text, int x, int y, int height) {
        int charHeight = height / text.length();
        Font font = g.getFont();
        FontMetrics fm = g.getFontMetrics(font);
        for (int i = 0; i < text.length(); i++) {
            String ch = String.valueOf(text.charAt(i));
            int textX = x;
            int textY = y + (i * charHeight) + fm.getAscent();
            g.drawString(ch, textX, textY);
        }
    }

    public static void main(String[] args) throws IOException {
        generateIcon("D:\\workspace\\icon\\test\\images\\background.png",
                "D:\\workspace\\icon\\test\\images\\logo.png",
                "知乎",
                "你好，世界",
                "13201609470",
                "D:\\workspace\\icon\\test\\images\\output.png");
    }
}


